package com.yzt.minemodule.adapter;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.zhuju.network.entity.mine.GiftListBean;
import com.yzt.minemodule.R;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import org.jetbrains.annotations.NotNull;

/**
 * author : bob
 * date   : 2022/12/7
 * desc   : 礼物墙适配器
 */
public class GiftWallAdapter extends BaseQuickAdapter<GiftListBean.GiftListDTO, BaseViewHolder> implements LoadMoreModule {

    public GiftWallAdapter() {
        super(R.layout.item_gift_wall);
    }

    @Override
    protected void convert(BaseViewHolder holder, GiftListBean.GiftListDTO bean) {

        holder.setText(R.id.tv_name, bean.getName());
        ImageView ivCover = holder.getView(R.id.img_view);
//        ImageLoadUtil.loadImg(getContext(), bean.getIcon(), ivCover);

        if (bean.isHave()) {
            holder.setTextColor(R.id.tv_name, ContextCompat.getColor(getContext(), R.color.white));
            ImageLoadUtil.load(getContext(), bean.getIcon(), ivCover,R.mipmap.icon_gift_default_gray);
        } else {
            holder.setTextColor(R.id.tv_name, ContextCompat.getColor(getContext(), R.color.color_6d7278));
           // ImageLoadUtil.load(getContext(), bean.getIcon(), ivCover,R.mipmap.icon_gift_default_gray);
            ImageLoadUtil.load4Drawable(getContext(), bean.getIcon(), new ImageLoadUtil.ImageRequestListener() {
                @Override
                public void onResourceReady(@NotNull Drawable resource) {
                    Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                    ivCover.setImageBitmap(grayScale(bitmap));
                }

                @Override
                public void onError() {

                }
            });
        }
    }

    /**
     * 图片灰度处理
     */
    public static Bitmap grayScale(Bitmap bitmap) {
        int width, height;
        height = bitmap.getHeight();
        width = bitmap.getWidth();
        Bitmap bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGray);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bitmap, 0, 0, paint);
        return bmpGray;
    }
}
